package com.yanqu.road.server.gameplayer.module.cricket.entity.dao;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.entity.DbUserCricketBattleLogDetail;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class UserCricketBattleLogDetailDaoImpl extends TempDataObjectDao<DbUserCricketBattleLogDetail> {


    public static UserCricketBattleLogDetailDaoImpl newInstance() {
        UserCricketBattleLogDetailDaoImpl dao = new UserCricketBattleLogDetailDaoImpl();
        return dao;
    }

    @Override
    protected boolean updateData(DbUserCricketBattleLogDetail data) {
        return true;
    }

    public static String insertSql = "insert into `t_u_cricket_battle_log_detail`(battleLogId,cricketBattleLog,createTime,updateTime) values (?,?,?,?)";

    @Override
    protected boolean addData(DbUserCricketBattleLogDetail data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getBattleLogId()));
        String encodeToString = Base64.getEncoder().encodeToString(data.getCricketBattleLog().toByteArray());
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, encodeToString));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }


    @Override
    protected DbUserCricketBattleLogDetail parseData(ResultSet rs) throws SQLException {
        DbUserCricketBattleLogDetail data = DbUserCricketBattleLogDetail.getInstance();
        data.setId(rs.getLong("id"));
        data.setBattleLogId(rs.getLong("battleLogId"));
        String cricketBattleLog = rs.getString("cricketBattleLog");
        byte[] decode = Base64.getDecoder().decode(cricketBattleLog);
        try {
            CricketProto.CricketBattleLog matchRoundGroup = CricketProto.CricketBattleLog.parseFrom(decode);
            data.setCricketBattleLog(matchRoundGroup);
        } catch (Exception e) {
            e.printStackTrace();
        }
        data.setCreateTime(rs.getLong("createTime"));
        data.setUpdateTime(rs.getLong("updateTime"));

        return data;
    }


    public DbUserCricketBattleLogDetail getData(long battleLogId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String selectSql = "select * from t_u_cricket_battle_log_detail where battleLogId = ? limit 1";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, battleLogId);
            rs = call.executeQuery();
            if (rs.next()) {
                DbUserCricketBattleLogDetail data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public boolean saveData(DbUserCricketBattleLogDetail data) {
        if (data.isUpdateOption()) {
            if (data.isNeedInsert()) {
                if (addData(data)) {
                    data.setNeedInsert(false);
                }
            }
        }
        return true;
    }

}
